package com.polaris.live.ui.live.activity.end.voice

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.StringUtils
import com.bumptech.glide.Glide
import com.gyf.immersionbar.ImmersionBar
import com.polaris.live.adapter.live.RoomEndAdapter
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.constant.AppConst
import com.polaris.live.common.ext.convertArabicDigitsToNormal
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.base2.BaseVbActivity
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.util.DateUtils
import com.polaris.live.constant.LiveConst
import com.polaris.live.databinding.ActivityVoiceEndBinding
import com.polaris.live.resp.back_resp.LiveStopReceiptResp
import com.polaris.live.resp.bean.CallEndBeansBean
import com.polaris.live.translation.R
import com.polaris.live.utils.CommonUtils
import com.polaris.live.utils.getIntOrString
import com.polaris.live.utils.image.ImageUtils
import jp.wasabeef.glide.transformations.BlurTransformation

/**
 * 派对关闭
 *
 * @author Created by 半仙 on 2024/2/29/029 20:53
 */
class VoiceEndActivity : BaseVbActivity<BaseViewModel, ActivityVoiceEndBinding>() {

    private val mLiveType by lazy { intent.getIntOrString("liveType", LiveConst.Type.PARTY_LIVE) }

    private val mAdapter by lazy { RoomEndAdapter() }

    override fun initImmersionBar() {
        ImmersionBar.with(this)
            .transparentStatusBar()
            .titleBar(mBinding.icBack)
            .fullScreen(false)
            .transparentNavigationBar()
            .init()
    }

    override fun initView(savedInstanceState: Bundle?) {
        val endInfo = intent.getParcelableExtra<LiveStopReceiptResp>(LiveConst.Params.LIVE_END_INFO)
        mBinding.apply {
            val user = UserManager.getUser()
            userAvatar.setImageUrlMiddle(user.avatar, user.userId)
            Glide.with(this@VoiceEndActivity)
                .load(ImageUtils.imgPath(user.avatar))
                .transform(BlurTransformation(15, 3))
                .into(backBigImg)

            if (user.profile?.secondCountryHidden == AppConst.YESLONG) {
                country.visibility = View.GONE
            } else {
                country.setCountry(user.profile?.secondCountry)
            }

            nickName.text = user.nickname
            mBinding.vipView.setVipLevel(user.profile?.vipLevel)
            val richs = user.account?.richs
            val charms = user.account?.charms
            if (richs != null && charms != null) {
                if (CommonUtils.isMan(user.sex)) {
                    otherLevel.setRichsLevel(richs)
                } else {
                    otherLevel.setCharmsLevel(charms)
                }
            }
            val list = mutableListOf<CallEndBeansBean>()
            if (endInfo != null) {
                val time = endInfo.duration.let { DateUtils.formatDuration(it) }
                    .convertArabicDigitsToNormal()
                list.add(
                    CallEndBeansBean(
                        StringUtils.getString(R.string.people_num),
                        endInfo.joinUser.toString(), false
                    )
                )
                if (mLiveType == LiveConst.Type.PARTY_GAME_LIVE) {
                    list.add(
                        CallEndBeansBean(
                            StringUtils.getString(R.string.game_income),
                            endInfo.gameGainCoins.toString(), true
                        )
                    )
                    list.add(
                        0,
                        CallEndBeansBean(
                            StringUtils.getString(R.string.living_time),
                            time, false
                        )
                    )
                } else {
                    list.add(
                        CallEndBeansBean(
                            StringUtils.getString(R.string.living_time),
                            time, false
                        )
                    )
                }
                list.add(
                    CallEndBeansBean(
                        StringUtils.getString(R.string.gifts_income),
                        endInfo.gainCoins.toString(), true
                    )
                )

                gainRecycler.apply {
                    adapter = mAdapter
                    layoutManager = GridLayoutManager(context, 2, GridLayoutManager.VERTICAL, false)
                }
                mAdapter.setList(list)
            }
        }
        initListener()
    }

    private fun initListener() {
        mBinding.confirm.setOnSingleClickListener {
            finish()
        }
    }

}